package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Label;
import java.util.ArrayList;


import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import framework.AnnuaireCocola;
import gui.utils.RoundedBorder;

public class ViewMetrique extends JPanel{

	private AnnuaireCocola cocola;
	private JLabel metrique;
	private ArrayList<String> top3Components;
	private ArrayList<String> top3InterfacesReq;
	private ArrayList<String> top3InterfacesPro;

	public ViewMetrique(AnnuaireCocola cocola){

		this.cocola = cocola;
		top3Components = new ArrayList<String>();
		top3InterfacesReq = new ArrayList<String>();
		top3InterfacesPro = new ArrayList<String>();

		this.setLayout(new BorderLayout());
		this.add(createComponent(),BorderLayout.CENTER);
		this.setBackground(new Color(230,230,230));
	}

	private JComponent createComponent(){

		Box mainbox = Box.createVerticalBox();

		metrique = new JLabel(getMetrique(),JLabel.CENTER);
		metrique.setFont(new Font("Andalus", Font.BOLD, 12));
		metrique.setBorder(new RoundedBorder(Color.BLACK,25,25));
		metrique.setForeground(Color.BLACK);
		metrique.setOpaque(true);
		metrique.setBackground(Color.WHITE);
		metrique.setPreferredSize(new Dimension(350,225));
		metrique.setMaximumSize(new Dimension(350,225));
		metrique.setMinimumSize(new Dimension(350,225));
		Box hbox = Box.createHorizontalBox();
		hbox.add(Box.createHorizontalGlue());
		hbox.add(metrique);
		hbox.add(Box.createHorizontalGlue());


		Box hbox2 = Box.createHorizontalBox();
		hbox2.add(Box.createHorizontalGlue());


		hbox2.add(createBigTitle("Library : "+cocola.getName()));



		hbox2.add(Box.createHorizontalGlue());




		Box hboxTop = Box.createHorizontalBox();
		hboxTop.add(Box.createHorizontalGlue());

		hboxTop.add(createTop("Top 5 Interfaces Required", getTop5InterReq()));
		hboxTop.add(Box.createHorizontalStrut(1));
		hboxTop.add(createTop("Top 5 Components", getTop5Component()));
		hboxTop.add(Box.createHorizontalStrut(1));
		hboxTop.add(createTop("Top 5 Interfaces Provided", getTop5InterPro()));

		hboxTop.add(Box.createHorizontalGlue());


		mainbox.add(Box.createVerticalStrut(100));
		mainbox.add(hbox2);
		mainbox.add(Box.createVerticalStrut(20));
		mainbox.add(hbox);
		mainbox.add(Box.createVerticalStrut(50));
		mainbox.add(hboxTop);
		mainbox.add(Box.createVerticalGlue());
		return mainbox;
	}

	private JComponent createTop(String title,ArrayList<String> a){

		Box mainBox = Box.createVerticalBox();

		JPanel panelLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JLabel label = new JLabel(title);
		label.setForeground(Color.WHITE);
		panelLabel.add(label);
		panelLabel.setBackground(Color.BLACK);
		panelLabel.setPreferredSize(new Dimension(400,25));
		panelLabel.setMaximumSize(new Dimension(400,25));

		JList list2 = new JList(a.toArray());
		JScrollPane slist2 = new JScrollPane(list2);
		slist2.setPreferredSize(new Dimension(400,250));
		slist2.setMaximumSize(new Dimension(400,250));


		mainBox.add(panelLabel);
		mainBox.add(Box.createVerticalStrut(10));
		mainBox.add(slist2);
		mainBox.add(Box.createVerticalGlue());

		return mainBox;
	}
	private ArrayList<String> getTop5InterPro(){
		ArrayList<String> entity = new ArrayList<String>();
		ArrayList<Integer> val = new ArrayList<Integer>();
		for (String key : cocola.getHierarchy().getInterfacesProvided().keySet()){
			int ival = cocola.getHierarchy().getInterfacesProvided().get(key).size();

			Boolean add = true;
			for (int i=0;i<val.size();i++){

				if ( val.get(i) < ival){
					add = false;
					entity.add(i,key);
					val.add(i, ival);
					break;
				}
			}
			if (add){
				
				val.add(ival);
				entity.add(key);
			}
		}
		ArrayList<String> result = new ArrayList<String>();
		
		for (int i=0;i<entity.size();i++){
			if (i > 4) break;
			result.add(entity.get(i));
		}

		return result;
	}
	private ArrayList<String> getTop5InterReq(){
			ArrayList<String> entity = new ArrayList<String>();
		ArrayList<Integer> val = new ArrayList<Integer>();
		for (String key : cocola.getHierarchy().getInterfacesRequired().keySet()){
			int ival = cocola.getHierarchy().getInterfacesRequired().get(key).size();

			Boolean add = true;
			for (int i=0;i<val.size();i++){

				if ( val.get(i) < ival){
					add = false;
					entity.add(i,key);
					val.add(i, ival);
					break;
				}
			}
			if (add){
				
				val.add(ival);
				entity.add(key);
			}
		}
		ArrayList<String> result = new ArrayList<String>();
		
		for (int i=0;i<entity.size();i++){
			if (i > 4) break;
			result.add(entity.get(i));
		}

		return result;
	}

	private ArrayList<String> getTop5Component(){
		ArrayList<String> entity = new ArrayList<String>();
		ArrayList<Integer> val = new ArrayList<Integer>();
		for (String key : cocola.getHierarchy().getComponentsToInterface().keySet()){
			int ival = cocola.getHierarchy().getComponentsToInterface().get(key).size();

			Boolean add = true;
			for (int i=0;i<val.size();i++){

				if ( val.get(i) < ival){
					add = false;
					entity.add(i,key);
					val.add(i, ival);
					break;
				}
			}
			if (add){
				
				val.add(ival);
				entity.add(key);
			}
		}
		ArrayList<String> result = new ArrayList<String>();
		
		for (int i=0;i<entity.size();i++){
			if (i > 4) break;
			result.add(entity.get(i));
		}

		return result;
	}
	
	private JComponent createBigTitle(String title){
		JPanel panelLabel = new JPanel();
		JLabel label = new JLabel(title);
		label.setForeground(Color.WHITE);
		label.setFont(new Font("Andalus", Font.BOLD, 22));
		panelLabel.add(label);
		panelLabel.setBackground(Color.BLACK);
		panelLabel.setPreferredSize(new Dimension(400,40));
		
		return panelLabel;
	}
	public String getMetrique(){
		int nbRealComponent = cocola.getHierarchy().getComponentsToInterface().keySet().size();

		int nbRealInterfaceReq = cocola.getHierarchy().getInterfacesRequired().keySet().size();
		int nbRealInterfacePro = cocola.getHierarchy().getInterfacesProvided().keySet().size();

		int nbFuncPro = cocola.getHierarchy().getFonctionsProvided().keySet().size();
		int nbFuncReq = cocola.getHierarchy().getFonctionsRequired().keySet().size();

		int nbParametrePrim = cocola.getHierarchy().getCocola().getPrimitives().size();
		int nbParametreNoPrim = cocola.getHierarchy().getCocola().getClasses().size();



		String comp = "Components : " + nbRealComponent+ "<br/><br/>";

		String inte1 = "Interfaces Provided : "+ nbRealInterfacePro + "<br/>";
		String inte2 = "Interfaces Required : "+ nbRealInterfaceReq + "<br/><br/>";

		String fonc1 = "Functions Provided : "+ nbFuncPro + "<br/>";
		String fonc2 = "Functions Required : "+ nbFuncReq + "<br/><br/>";


		String fonc5 = "Primitives parametres : "+ nbParametrePrim ;
		String fonc6 = "Classes : "+ nbParametreNoPrim+"<br/>";


		return "<html>"+comp+inte1+inte2+fonc1+fonc2+fonc6+fonc5+"</html>";
	}



}
